/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.User;
import java.sql.ResultSet;
import java.util.List;
import DTO.CFields;
import DTO.CInFoUser;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.CNhanVien;
/**
 *
 * @author quangphuc
 */
public class UserDAO extends IData<User>{
    public UserDAO(){
    }
    @Override
    protected Object OnSubmit(User item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected User getItem(ResultSet rs) {
        User item = new User();
        try {
            item.setGroupUser(rs.getInt(CFields.group_id));
            item.setActive(rs.getBoolean(CFields.active));
            item.setPassword(rs.getString(CFields.password));
            item.setUserName(rs.getString(CFields.user_name));
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<User> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_user;
        return this.getAll(mysql, config);
    }
    
    public User logIn(String username,String password,SubConfig config){
        List<User> users = getAll(config);
        for(User item : users){
            String _user = item.getUserName().toLowerCase().trim();
            String _pass = item.getPassWord().toLowerCase().trim();
            if(item.getPassWord().equals(password.toLowerCase()) && item.getUserName().equals(username.toLowerCase()))
                return item;
        }
        return null;
    }

    @Override
    public Object insertOnSubmit(User item, SubConfig config) {
       Object result = 0;
       String mysql ="INSERT INTO " + CFields.tb_user  + "("+ CFields.user_name 
               + "," + CFields.password+","+CFields.group_id +"," + CFields.active +") VALUES(?,?,?,?)";
        System.out.println("m " + mysql);
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setString(1, item.getUserName());
            cs.setString(2, item.getPassWord());
            cs.setLong(3,item.getGroupUser());
            cs.setBoolean(4, item.getActive());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       return result;
    }

    @Override
    public Object updateOnSubmint(User item, SubConfig config) {
       Object result = 0;
       String mysql ="update " + CFields.tb_user + " set " + CFields.password + "=?,"
               + CFields.active+"=?,"+ CFields.group_id+"=? where id=?";
        System.out.println("m " + mysql);
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setString(1, item.getPassWord());
            cs.setBoolean(2, item.getActive());
            cs.setLong(3,item.getGroupUser());
            cs.setLong(4,item.getId());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       return result;
    }

    @Override
    public Object deleteOnSubmint(User item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User Find(User item, List<User> list) {
        User iFind = null;
        for(User k : list){
            if(k.getUserName() == item.getUserName() && k.getPassWord() == item.getPassWord()){
                iFind = k;
                break;
            }
        }
        return iFind;
    }

    public CInFoUser getInFoUser(User item,SubConfig config){
        CInFoUser user = new CInFoUser();
        CNhanVienDAO nv = new CNhanVienDAO();
        List<CNhanVien> list = nv.getAll(config);
        for(CNhanVien _nv : list){
            if(_nv.getUserId() == item.getId()){
                user.setHoTen(_nv.getHoTen());
                user.setId(item.getId());                
            }
        }
        return user;
    }

    @Override
    public User add(User item) {
        try {
            ps.setString(1, item.getPassWord());
            ps.setBoolean(2, item.getActive());
            ps.setLong(3,item.getGroupUser());
            ps.setLong(4,item.getId());
            ps.addBatch();            
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<User> list, SubConfig config) {
        String mysql ="INSERT INTO " + CFields.tb_user  + "("+ CFields.user_name 
               + "," + CFields.password+","+CFields.group_id +"," + CFields.active +") VALUES(?,?,?,?)";
        try {
            ps = config.connec.prepareStatement(mysql);
            for(User item : list){
                this.add(item);
            }
            ps.executeBatch();
            ps.close();
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
